package com.opal.modules.costReportForm.controller;

import com.opal.consts.WSC;
import com.opal.domain.ReturnMessageData;
import com.opal.modules.costReportForm.entity.table.CostApprovals;
import com.opal.modules.costReportForm.service.ApprovalTaskService;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.bpmn.model.FlowElement;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Collection;

//@CrossOrigin(origins = "*", maxAge = 3600)
@Controller
@RequestMapping("/examine")
public class ApprovalTaskController {

    @Autowired
    private ApprovalTaskService approvalTaskService;

    @Autowired
    private ProcessEngine processEngine;


    /**
     * 提交审批
     *
     * @param id     业务id
     * @param flowId 流程图id
     * @return
     */
    @RequestMapping("submitFile/{flowId}/{id}")
    @ResponseBody
    public ReturnMessageData submitFile(@PathVariable("id") String id, @PathVariable("flowId") String flowId, @PathVariable("grossMarginRate") BigDecimal grossMarginRate) throws Exception {

        ReturnMessageData rmd = approvalTaskService.submitFileService(id, flowId,null);

        return rmd;
    }

    /**
     * 点击菜单栏：审批【成本报告表】获取数据
     * @param flowId 流程id
     * @return
     */
    @RequestMapping("queryselftask/{flowId}")
    @ResponseBody
    public Object querySelfTask(@PathVariable("flowId") String flowId) {
        Object obj=approvalTaskService.querySelfTaskService(flowId);
        return obj;
    }

    /**
     * 审批成本报告
     *
     * @param num               1同意，0拒绝,2驳回
     * @param costApprovals 实体类
     * @return
     */
    @RequestMapping("approvalprocess")
    @ResponseBody
    public ReturnMessageData approvalProcess(CostApprovals costApprovals,Integer num,Integer result,Integer resubmit) {
        ReturnMessageData rmd = approvalTaskService.approvalProcessService(costApprovals,num,result,resubmit);

        return rmd;
    }

    /**
     * 查询个人提交流程实例
     * @return  返回任务进展详情
     */
    @RequestMapping("queryelfsubmittask")
    @ResponseBody
	public Object querySelfSubmiTtask(int page,int rows) {
		Object obj=approvalTaskService.querySelfSubmiTtaskService(page,rows);
		return obj;
	}

    /**
     * 查询个人审批的历史详情
     * @return  返回任务进展详情
     */
    @RequestMapping("queryCompletionTask")
    @ResponseBody
	public Object queryCompletionTaskService(int page,int rows) {
		Object obj=approvalTaskService.queryCompletionTaskService(page,rows);
		return obj;
	}

    /**
     * 根据业务id查询当前流程实例对象(所有的,也就是包括运行中的)
     * @param business
     * @return
     */
    @RequestMapping()
    @ResponseBody
    public Object queryHistoryProcessByBusiness(String business){
      return   approvalTaskService.queryHistoryProcessByBusiness(business);
    }

    /**
     * 部署流程定义至数据库中
     * act_re_procdef\act_ge_bytearray\act_re_deployment
     *
     * @return
     */
    @RequestMapping("a")
    @ResponseBody
    public String created() {
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment d = repositoryService.createDeployment()
                .addClasspathResource("static/bpmn/costReport.bpmn")
                .addClasspathResource("static/bpmn/costReport.bpmn")
                .name("成本报表导出")
                .category("分类")
                .key("自定义的key")
                .deploy();

        return "部署的流程id：" + d.getId() + "部署的名字：" + d.getName();
    }

    @RequestMapping("getAllProcessInstanceTask/{processInstanceId}")
    @ResponseBody
    public  Object getAllProcessInstanceTask(@PathVariable("processInstanceId")String processInstanceId){
        return approvalTaskService.getAllProcessInstanceTaskService(processInstanceId);
    }

    @Autowired
    private RepositoryService repositoryService;

    @RequestMapping("tx/{modelId}/{nodeId}")
    @ResponseBody
    public String getNodeBaseElement(@PathVariable("modelId") String processDefinitionId, @PathVariable("nodeId") String nodeId) {
        BpmnModel model = repositoryService.getBpmnModel(processDefinitionId);
        if(model != null) {
            Collection<FlowElement> flowElements = model.getMainProcess().getFlowElements();
            for(FlowElement e : flowElements) {
                System.out.println("flowelement id:" + e.getId() + "  name:" + e.getName() + "   class:" + e.getClass().toString());
            }
        }
        return "呵呵";

    }

    @ExceptionHandler({RuntimeException.class})
    @ResponseBody
    public Object runtimeException(Exception e){

        return new ReturnMessageData(WSC.RESTPONSE_OK,e.getMessage());
    }

}
